import Mock from 'mockjs'
import List from './tableData'

const domain = 'http://mockjs.com/api' 
const token="ceshi"

Mock.setup({
    timeout: 0-300
})

function login(params){
    const {username,password}=JSON.parse(params.body);
    if(username==="admin"&&password==="111111"){
        return {code:1,token}
    }else{
        return {code:0, message: "用户名或者密码错误"}
    }
}

function getUserInfo(){
    var datas={
        code:1,
        userInfo:{username:"marcus",roles:'Admin'},
        pagePermission:[
            {menuName:'RoleManage',pagePermissions:30},
            {menuName:'Menu1-2-1',pagePermissions:17},
            {menuName:'Menu2',pagePermissions:31},
            {menuName:'Table',pagePermissions:17}
        ]
    };
    return {code:datas.code,userInfo:datas.userInfo,pagePermission:datas.pagePermission}
    //return {code:1,userInfo:{username:"marcus"},roles:["admin"]}
}

function getRoles(){
    const data=[
        {
            role:"VP of Business",
            ModifiedDate:"2019-08-13",
            ModifiedBy:"Admin" 
        },
        {
            role:"Operations Director",
            ModifiedDate:"2019-08-13",
            ModifiedBy:"Admin" 
        },
        {
            role:"Business Unit Manager",
            ModifiedDate:"2019-08-13",
            ModifiedBy:"Admin" 
        },
        {
            role:"Quality",
            ModifiedDate:"2019-08-13",
            ModifiedBy:"Admin"  
        },
        {
            role:"Master Planner",
            ModifiedDate:"2019-08-13",
            ModifiedBy:"Admin"  
        },
        {
            role:"Admin",
            ModifiedDate:"2019-08-13",
            ModifiedBy:"Admin"    
        }
    ]
    return {code:1,roles:data}
}

function getTableList(params){
    const {page,limit,title,status,sort}=JSON.parse(params.body);
    let mockList=List.filter(item=>{
        if(status&&item.status!==status) return false
        if(title&&item.title.indexOf(title)<0) return false
        return true
    })
    if (sort === '-id') {
        mockList = mockList.reverse()
      }
     const pageList=mockList.filter((item,index)=>index < limit * page && index >= limit * (page - 1)) 
     return {
         code:1,
         data:{
            total: mockList.length,
            items: pageList
         }
     }
}

Mock.mock(`${domain}/login`, 'post', login); //登录
Mock.mock(`${domain}/getUserInfo`,'get',getUserInfo)//获取用户信息
Mock.mock(`${domain}/getRoles`,'get',getRoles)//获取角色信息

Mock.mock(`${domain}/getTableList`, 'post', getTableList) //模拟表格数据
